﻿///////////////////////////////////////////////////
/// toto Code
/// $Id: cond.h 3 2009-12-14 12:46:10Z tonyjobmails $
/// $Date: 2009-12-14 20:46:10 +0800 (一, 14 12月 2009) $
/// $Author: tonyjobmails $
/// $Revision: 3 $
/// E-mail: tonyjobmails@gmail.com
/// filename: cond.h
/// function: 条件变量定义
///////////////////////////////////////////////////

#ifndef __COND__H
#define __COND__H

#include <pthread.h>
#include "Mutex.h"

class CCond
{
	public:
		CCond(const int _kind = PTHREAD_PROCESS_PRIVATE)
		{
			::pthread_condattr_t attr;
			::pthread_condattr_init(&attr);
			::pthread_condattr_setpshared(&attr, _kind);
			::pthread_cond_init(&this->cond, &attr);
		}

		virtual ~CCond()
		{
			::pthread_cond_destroy(&this->cond);
		}

		/// 等待条件
		void wait(CMutex &_mutex)
		{
			::pthread_cond_wait(&this->cond, &_mutex.mutex);
		}

		/// 条件满足，激活第一个线程
		void signal()
		{
			::pthread_cond_signal(&this->cond);
		}

		/// 激活队列中全部线程
		void broadcast()
		{
			::pthread_cond_broadcast(&this->cond);
		}

	private:
		/// 条件变量
		pthread_cond_t cond;
};

#endif	/// __COND__H
